package org.example;

import org.example.Voutils.CallableDemo;
import org.example.Voutils.RunnableDemo;
import org.example.Voutils.ThreadDemo;
import org.example.Voutils.Ticket;

import java.util.concurrent.*;
import java.util.function.Supplier;

// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`，
// 然后按 Enter 键。现在，您可以在代码中看到空格字符。
public class Main {
//    public static void main(String[] args) {
//
//        //ThreadDemo
//   // new ThreadDemo().start();
//
//        //RunnableDemo
////        RunnableDemo runnableDemo = new RunnableDemo();
////        Thread thread = new Thread(runnableDemo);
////        thread.start();
//
//        //CallableDemo
////        CallableDemo callableDemo = new CallableDemo();
////        FutureTask futureTask = new FutureTask<>(callableDemo);
////        Thread thread = new Thread(futureTask, "CallableDemo");
////        thread.start();
////        try {
////            Object o = futureTask.get();
////            System.out.println(o+"");
////        } catch (InterruptedException e) {
////            throw new RuntimeException(e);
////        } catch (ExecutionException e) {
////            throw new RuntimeException(e);
////        } finally {
////        }
//
//        //Ticket demo
////        Ticket ticket = new Ticket();
////        new Thread(()->{
////            for (int i = 0; i < 21; i++) {
////                ticket.sale1();
////            }
////        },"A").start();
////        new Thread(() -> {
////            for (int i = 0; i < 21; i++) {
////                ticket.sale1();
////            }
////        }, "B").start();
////        new Thread(() -> {
////            for (int i = 0; i < 21; i++) {
////                ticket.sale1();
////            }
////        }, "C").start();
////
////        new Thread(() -> {
////            for (int i = 0; i < 21; i++) {
////                ticket.sale2();
////            }
////        }, "A2").start();
////        new Thread(() -> {
////            for (int i = 0; i < 21; i++) {
////                ticket.sale2();
////            }
////        }, "B2").start();
////        new Thread(() -> {
////            for (int i = 0; i < 21; i++) {
////                ticket.sale2();
////            }
////        }, "C2").start();
//    }
public static void main(String[] args) {
    ExecutorService executorService = Executors.newFixedThreadPool(3);
//    Runnable runnable = ()-> {
//        System.out.println(Thread.currentThread().getName()+"正在执行");
//    };
    Supplier<String> supplier = ()->{
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
        }
        System.out.println(Thread.currentThread().getName()+"正在执行");
        return "amin++#";
    };
   // CompletableFuture<String> Future = CompletableFuture.supplyAsync(supplier, executorService);
    //    try {
//        String s = Future.get();
//        System.out.println(s);
//    } catch (InterruptedException e) {
//        throw new RuntimeException(e);
//    } catch (ExecutionException e) {
//        throw new RuntimeException(e);
//    } finally {
//    }
    CompletableFuture<String> future = CompletableFuture.supplyAsync(supplier, executorService);
    // 3.5、在另一个线程中通过调用 complete() 方法强制结束任务
    new Thread(() -> {

        try {
            TimeUnit.SECONDS.sleep(3);

            boolean completeResult = future.complete("specialValue");
            System.out.println("completeResult = " + completeResult);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }, "thread-another").start();

    future.whenComplete((t,u)->{
        System.out.println(t);
    });



    executorService.shutdown();
}
}